/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.uct.common.entity.Sys;
import org.apache.log4j.Logger;

/**
 * description:  获取系统列表
 *
 * @author Hou Dayu 创建时间：2016-2-2
 */
public class SysListPlugin implements PluginConnector {

    private Logger log = Logger.getLogger(SysListPlugin.class);

    private IDao sysDao;

    /**
     * TODO 获取系统信息列表插件.  <br>
     * 参数说明:<br>
     * sys属性:按系统信息查询列表;<br>
     * 分页设置:<br>
     * pageSize:每页记录数;<br>
     * pageIndex:当前页码;
     */
    public Object execute(ParamsVo vo) throws Exception {
        log.info(vo.getParams());
        ResultEntity result = new ResultEntity();
        try {
            result.setResult("true");
            MethodParam param = setMethodParams(vo, 2);
            if (Validator.isEmpty(param)) {
                result.setMessage("查询条件为空或不存在相关信息");
                return result;
            }

            int pageSize = Integer.valueOf(Validator
                    .getDefaultStr(String.valueOf(vo.getParams("pageSize")),
                            Validator.getDefaultStr(String.valueOf(vo.getParams("rows")), "0")));
            int pageIndex = Integer.valueOf(Validator
                    .getDefaultStr(String.valueOf(vo.getParams("pageIndex")),
                            Validator.getDefaultStr(String.valueOf(vo.getParams("page")), "0")));
            if (pageSize == 0 && pageIndex == 0) {
                param.setOrderby(" order_num asc");
                result.setEntity(sysDao.select(param));
            } else {
                param.setPageIndex(pageIndex);
                param.setPageSize(pageSize);
                param.setOrderby(" order_num asc");
                Page page = sysDao.pageSelect(param);
                result.setEntity(page);
            }
        } catch (Exception e) {
            result.setResult("false");
            log.error("执行机构查询失败 ", e);
            result.setMessage("机构查询失败！");
        }
        return result;
    }

    private MethodParam setMethodParams(ParamsVo vo, int type) {
        StringBuffer sb = new StringBuffer(sysDao.getSql(type));
        MethodParam param = new MethodParam("ByProperty", "", "", Sys.class.getName());
        String sysAll = String.valueOf(vo.getParams("sysAll"));
        if (Validator.isNull(sysAll)) {
            String str = buildParams(param, vo);
            if (Validator.isNull(str))
                return null;
            sb.append(str);
        }

        sb.append(" order by orderNum");
        param.setSqlStr(sb.toString());
        return param;
    }

    private String buildParams(MethodParam param, ParamsVo vo) {
        StringBuffer sb = new StringBuffer();
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysCname")))) {
            sb.append(" and sysCname like :sysCname");
            param.setParams("sysCname", "'%" + String.valueOf(vo.getParams("sysCname")) + "%'");
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
            sb.append(" and sysEname like :sysEname");
            param.setParams("sysEname", "'%" + String.valueOf(vo.getParams("sysEname")) + "%'");
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("lockType")))) {
            sb.append(" and checkIp = :checkIp");
            param.setParams("checkIp", String.valueOf(vo.getParams("checkIp")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("allowIplist")))) {
            sb.append(" and allowIplist = :allowIplist");
            param.setParams("allowIplist", String.valueOf(vo.getParams("allowIplist")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysStatus")))) {
            sb.append(" and sysStatus = :sysStatus");
            param.setParams("sysStatus", String.valueOf(vo.getParams("sysStatus")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysRemark")))) {
            sb.append(" and sysRemark = :sysRemark");
            param.setParams("sysRemark", String.valueOf(vo.getParams("sysRemark")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("delStatus")))) {
            sb.append(" and delStatus = :delStatus");
            param.setParams("delStatus", String.valueOf(vo.getParams("delStatus")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("orgId")))) {
            sb.append(" and orgId = :orgId");
            param.setParams("orgId", String.valueOf(vo.getParams("orgId")));
        }
        return sb.toString();
    }

    public void setSysDao(IDao sysDao) {
        this.sysDao = sysDao;
    }
}
